#!/bin/sh

# Copyright (C) 2008, David Smid <david@smidovi.eu>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


. gettext.sh

export TEXTDOMAIN=command-not-found

shopt -u extquote

CACHE=/var/cache/cnf/cnfcache.sqlite

if [ -L "$0" ]
then
    if [ ! -r "$CACHE" ]
    then
	PATH="" exec $0
    fi
    COMMAND=`basename $0`
    PROGRAMS=( `sqlite3 $CACHE "select value from tag where name='files' and ( value like '%/bin/$COMMAND' or value like '%/sbin/$COMMAND' )"` )
    PACKAGES=( `sqlite3 $CACHE "select package_name from tag where name='files' and ( value like '%/bin/$COMMAND' or value like '%/sbin/$COMMAND' )"` )
    IFS=":" PATHLIST=( $PATH )

    for PROGRAM in "${PROGRAMS[@]}"
    do
	PROGDIR=`dirname $PROGRAM`
	FOUNDINPATH1=`expr "$PATH" : ".*[:]\\($PROGDIR\\)[:].*"`
	FOUNDINPATH2=`expr "$PATH" : "\\($PROGDIR\\)[:].*"`
	FOUNDINPATH3=`expr "$PATH" : ".*[:]\\($PROGDIR\\)"`
	if [ -o "$FOUNDINPATH1" -a -x "$PROGRAM" -o -n "$FOUNDINPATH2" -a -x "$PROGRAM" -o -n "$FOUNDINPATH3" -a -x "$PROGRAM" ]
	then
	    exec $PROGRAM $@
	fi
	if [ -x "$PROGRAM" ]
	then
	    printf $"Program \"%s\" is installed but is not in your PATH.\n" $PROGRAM
	    ISINSTALLED=1
	else
	    if [ -r "$PROGRAM" ]
	    then
		printf $"Program \"%s\" is installed but is not executable.\n" $PROGRAM
		ISINSTALLED=1
	    fi
	fi
    done

    if [ "$ISINSTALLED" == "1" ]
    then
	exit 127
    fi

    for PCK in ${PACKAGES[@]}
    do
	INSTALLED=`rpm -q $PCK`
	if [ "$?" == "0" ]
	then
	    RESULT=$?
	    break;
	fi
    done
    if [ "$RESULT" == "0" ]
    then
	printf $"The package containing \"%s\" is installed ( %s ), but the program is missing. Weird.\n" $COMMAND $INSTALLED
	exit 127
    fi
    printf $"Program \"%s\" is not installed.\n" $COMMAND
    ngettext "It's part of the following package:" "It's part of the following packages:" ${#PACKAGES[@]}
    echo
    for PCK in ${PACKAGES[@]}
    do
	echo "---------------------------------------------------------------------"
	(
	printf $"Package:\t"
	sqlite3 $CACHE "select name,version,release,arch from package where name = '$PCK' order by version desc, release desc limit 1" | awk 'BEGIN {FS="|"}{print $1 "-" $2 "-" $3 "." $4}'
	printf $"Description:\t"
	sqlite3 $CACHE "select value from tag where name='summary' and package_name = '$PCK'"
	printf $"Install by typing:\t"
	printf "su -c 'apt-get install $PCK'\n"
	) | expand -t 25
    done
    echo "---------------------------------------------------------------------"
    exit 127
else
    printf $"This program should not be run directly.\n"
fi
